﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jzgl
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
        }
        private void button3_Click(object sender, EventArgs e)//退出按钮
        {
            DialogResult dr = MessageBox.Show("确定要退出？", "", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        private void RegButton_Click(object sender, EventArgs e)//注册
        {
            this.Visible = false;
            Register re = new Register();
            re.ShowDialog();
        }
        private void button1_Click(object sender, EventArgs e)//登陆
        {
            try
            {
                DataClasses1DataContext DB = new DataClasses1DataContext();
                if (comboBox1.SelectedItem.ToString() == "普通用户")
                {
                    Users user = DB.Users.Single(p => p.USERNAME.ToString().Trim() == NameBox.Text);
                    if (user.PASSWORD.ToString().Trim() == PswdBox.Text)
                    {
                        if (user.STATE == 1)
                        {
                            MessageBox.Show("登录成功");
                            this.Visible = false;
                            UsersWindow re = new UsersWindow(user.LICENSENUM);
                            re.ShowDialog();
                        }
                        else { MessageBox.Show("用户未激活，请等待管理员审核"); }
                    }
                    else
                    {
                        MessageBox.Show("密码错误");
                    }                    
                }
                else
                {
                    Admin user = DB.Admin.Single(p => p.USERNAME.ToString().Trim() == NameBox.Text);
                    if (user.PASSWORD.ToString().Trim() == PswdBox.Text)
                    {
                        MessageBox.Show("登录成功");
                        this.Visible = false;
                        AdminWindow re = new AdminWindow();
                        re.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("密码错误");
                    }
                }
            }
            catch
            {
                MessageBox.Show("查无此账号");
            }
        }
        private void PswdBox_KeyDown(object sender, KeyEventArgs e)//回车指向登陆按钮
        {
            if (e.KeyCode == Keys.Enter)
                button1.PerformClick(); 
        }
        private void NameBox_KeyDown(object sender, KeyEventArgs e)//回车指向登陆按钮
        {
            if (e.KeyCode == Keys.Enter)
                button1.PerformClick(); 
        }
    }
}
